package cn.net.awit.API.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.system.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.net.awit.API.entity.JSONResult;
import cn.net.awit.weaforcast.entity.WeaForcastEntity;

/**   
 * @Title: Controller  
 * @Description: LED屏幕查看
 * @author onlineGenerator
 * @date 2018-10-29 09:33:37
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/led")
public class LedScreenController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(LedScreenController.class);

	@Autowired
	private UserService userService;
	@Autowired
	private SystemService systemService;
	
	/**
	 * 跳转主页
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception{
		return new ModelAndView("cn/net/awit/ledScreen/ledScreenPage");
	}

	@RequestMapping(params = "getForcast")
	@ResponseBody
	public JSONResult getForcast(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONResult jsonResult = new JSONResult(0, "");
		try {
			List<Map<String, Object>> findForJdbc = systemService.findForJdbc("select b.num,b.lat,b.lat,b.id,a.time,a.rain,a.snow,a.cloud,a.temp,a.rh,a.vis,a.p,a.wd,a.ws " 
					+"from wea_forcast_data as a,wea_forcast as b "
					+"where a.forcast_id=b.id and b.status=1 "
					+"and time between DATEADD(HOUR,-1,GETDATE()) and GETDATE() order by b.id");
			jsonResult.addObj("data", findForJdbc);
		} catch (Exception e) {
			jsonResult.setCode(0);
			jsonResult.setMsg("数据获取失败！");
			throw e;
		}
		return jsonResult;
	}
	
	@RequestMapping(params = "getDisaster")
	@ResponseBody
	public JSONResult getDisaster(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONResult jsonResult = new JSONResult(0, "");
		String[] keyName=new String[]{"rain","snow","vis","freez"};
		int[] type=new int[]{1,2,4,5};
		try {
			Map<String, Map<String, Object>> data=new HashMap<>();
			for(int i=0;i<keyName.length;i++){
				List<Map<String, Object>> objData = systemService.findForJdbc("exec proc_wewather_statu @forcast_id=?,@disaster=?,@hour=?,@when=?",
						null,type[i],3,0);
				for(Map<String, Object> map:objData){
					if(map.get("id")!=null){
						String id=(String) map.get("id");
						if(data.get(id)==null){
							data.put(id, map);
						}
						data.get(id).put(keyName[i], map.get("val"));
						if(i==3){
							data.get(id).put("ct", map.get("ct"));
						}
					}
				}
			}
			jsonResult.addObj("data", data);
		} catch (Exception e) {
			jsonResult.setCode(0);
			jsonResult.setMsg("数据获取失败！");
			throw e;
		}
		return jsonResult;
	}
	
	/**
	 * 获取气象站的最新数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "getStationInfo")
	@ResponseBody
	public JSONResult getStationInfo(HttpServletRequest request){
		JSONResult jsonResult = new JSONResult();
		try {
			List<Map<String, Object>> resData=new ArrayList<>();
			List<Map<String, Object>> dataTimeList = systemService.findForJdbc("select station_code,MAX(data_time) as data_time from wea_station_data "
					+"group by station_code having station_code in (select station_code from wea_station where station_status=1) ");
			for(Map<String, Object> map:dataTimeList){
				String stationCode = (String) map.get("station_code");
				Date dataTime = (Date) map.get("data_time");
				List<Map<String, Object>> s = systemService.findForJdbc("select a.*,b.station_address,b.station_lat,b.station_lng from wea_station_data as a,wea_station as b "
						+"where a.station_code=b.station_code "
						+"and a.station_code=? "
						+"and a.data_time=?", stationCode,dataTime);
				if(s.size()>0){
					resData.add(s.get(0));
				}
			}
			jsonResult.addObj("data", resData);
			jsonResult.setCode(200);
		} catch (Exception e) {
			jsonResult.setCode(203);
			jsonResult.setMsg(e.getMessage());
			systemService.addApiLog("led/getStationInfo：err:"+e.getMessage(), Globals.Log_Type_OTHER, Globals.Log_Leavel_ERROR,null);
		}
		return jsonResult;
	}
	
	
}
